package com.biubiushop.socket.bio;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**TCP短连接示例
 * @author chenjia
 * @date 2019/7/16
 **/
@Component
public class SocketServer {

    private static ThreadPoolTaskExecutor taskExecutor;

    public ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
        return taskExecutor;
    }

    @Resource
    public void setThreadPoolTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
        SocketServer.taskExecutor = taskExecutor;
    }

    public static void run() throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999);

        int count = 0;
        System.out.println("============服务端即将启用===========");
        Socket socket = null;
        while (true){
            socket = serverSocket.accept();
            taskExecutor.execute(new SocketThread(socket));
            count++;
            System.out.println("客户端的数量:"+count);
            System.out.println("活动的线程数:"+taskExecutor.getActiveCount());
            System.out.println("总线程数:"+taskExecutor.getPoolSize());
            System.out.println("核心线程数:"+taskExecutor.getCorePoolSize());

            InetAddress inetAddress = socket.getInetAddress();
            String ip = inetAddress.getHostAddress();
            System.out.println(ip);
        }
    }

}
